using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Xml;

using SharpWind.Resources;
using SharpWind.Tools;

using Font=SharpWind.Resources.Font;

namespace SharpWind.Gui
{
	public class Theme
	{
		#region Control

		internal class Control
		{
			#region Element

			internal class Element
			{
				private string name;

				public string Name
				{
					get
					{
						return name;
					}
					set
					{
						name = value;
					}
				}

				private int x;

				public int X
				{
					get
					{
						return x;
					}
					set
					{
						x = value;
					}
				}

				private int y;

				public int Y
				{
					get
					{
						return y;
					}
					set
					{
						y = value;
					}
				}

				private int width;

				public int Width
				{
					get
					{
						return width;
					}
					set
					{
						width = value;
					}
				}

				private int height;

				public int Height
				{
					get
					{
						return height;
					}
					set
					{
						height = value;
					}
				}

				private Color color;

				public Color Color
				{
					get
					{
						return color;
					}
					set
					{
						color = value;
					}
				}
			}

			#endregion

			private Dictionary<string, Element> elements = new Dictionary<string, Element>();

			public Dictionary<string, Element> Elements
			{
				get
				{
					return elements;
				}
			}
		}

		#endregion

		private string name;

		public string Name
		{
			get
			{
				return name;
			}
			private set
			{
				name = value;
			}
		}

		private Font font = null;

		public Font Font
		{
			get
			{
				return this.font;
			}
			private set
			{
				this.font = value;
			}
		}

		private Theme( string theme )
		{
			ParseThemeFile( theme );
		}

		private void ParseThemeFile( string theme )
		{
			if( theme != null && theme != string.Empty && File.Exists( Directories.Themes + theme ) )
			{
				XmlDocument doc = new XmlDocument();
				doc.Load( Directories.Themes + theme );

				#region Theme

				XmlNode node = doc.SelectSingleNode( "/theme" );
				Name		 = node.Attributes[0].Value;
				this.texture = Engine.Instance.Content.Textures.Load( node.Attributes[1].Value );

				string face = node.Attributes[2].Value;
				int size    = int.Parse( node.Attributes[3].Value );
				bool bold   = int.Parse( node.Attributes[4].Value ) == 0 ? false : true;
				bool italic = int.Parse( node.Attributes[5].Value ) == 0 ? false : true;

				Font = new Font( face, size, bold, italic );

				Engine.Instance.Content.Fonts.Add( "GuiDefaultFont", Font );

				#endregion

				foreach( XmlNode c in doc.SelectNodes( "/theme/control" ) )
				{
					Control control = new Control();

					foreach( XmlNode n in doc.SelectNodes( @"/theme/control[@name='" + c.Attributes[0].Value + @"']/element" ) )
					{
						Control.Element element = new Control.Element();
						element.Name = n.Attributes[0].Value;
						element.X = int.Parse( n.Attributes[1].Value );
						element.Y = int.Parse( n.Attributes[2].Value );
						element.Width = int.Parse( n.Attributes[3].Value );
						element.Height = int.Parse( n.Attributes[4].Value );
						element.Color = ColorTranslator.FromHtml( n.Attributes[5].Value );

						control.Elements.Add( element.Name, element );
					}

					Controls.Add( c.Attributes[0].Value, control );
				}
			}
		}

		private Dictionary<string, Control> controls = new Dictionary<string, Control>();

		internal Dictionary<string, Control> Controls
		{
			get
			{
				return controls;
			}
		}

		private Texture texture = null;

		internal Texture Texture
		{
			get
			{
				if( texture.D3DTexture == null )
				{
					this.texture.D3DTexture = Engine.Instance.Content.Textures[this.texture.Name].D3DTexture;
				}
					
				return this.texture;
			}
			set
			{
				this.texture = value;
			}
		}

		public static Theme Load( string theme )
		{
			return new Theme( theme );
		}

		public static Theme Default
		{
			get
			{
				return Load( "default.theme" );
			}
		}
	}
}